package com.ohc.itnanls;

public class SuperLinked {
    private Node head;

    private Integer currentIndex = -1;

    public void addToHead(int data) {
        this.add(0, data);
    }

    public void addToTail(int data) {
        this.add(currentIndex + 1, data);
    }

    public void add(int index, int data) {
        if (head == null) {
            head = new Node(data, null);
        } else {
            if (index == 0) {
                Node node = new Node(data, head);
                head = node;
            } else {
                Node node = selectNode(index - 1);
                Node newNode = new Node(data, node.getNext());
                node.setNext(newNode);
            }
        }
        currentIndex++;
    }

    public void delete(int index) {
        if (index == 0) {
            head = head.getNext();
        } else {
            Node node = selectNode(index - 1);
            node.setNext(node.getNext().getNext());
        }
        currentIndex--;

    }

    public void set(int index, int data) {
        selectNode(index).setData(data);
    }

    public int select(int index) {
        return selectNode(index).getData();
    }

    public int size() {
        return currentIndex + 1;
    }

    private Node selectNode(int index) {
        Node node = head;
        for (int i = 0; i < index; i++) {
            node = node.getNext();
        }
        return node;
    }

    //    6 5 4 3
//     5 4 3 6
//    4 3 5 6
//    3 4 5 6
    public void sort1() {
        for (int i = 0; i < currentIndex; i++) {
            for (int j = 0; j < currentIndex - i; j++) {
                if (selectNode(j).getData() > selectNode(j + 1).getData()) {
                    int temp = selectNode(j).getData();
                    selectNode(j).setData(selectNode(j + 1).getData());
                    selectNode(j + 1).setData(temp);
                }
            }
        }
    }

    public void sort2() {
        for (int i = 0; i < currentIndex; i++) {
            for (int j = 0; j < currentIndex - i; j++) {
                Node node = selectNode(j);
                Node next = node.getNext();
                if (node.getData() > next.getData()) {
                    int temp = node.getData();
                    node.setData(next.getData());
                    next.setData(temp);
                }
            }
        }
    }

    public void sort3() {
        for (int i = 0; i < currentIndex; i++) {
            Node node = null;
            for (int j = 0; j < currentIndex - i; j++) {
                if (j == 0) {
                    node = head;
                } else {
                    node = node.getNext();
                }
                Node next = node.getNext();
                if (node.getData() > next.getData()) {
                    int temp = node.getData();
                    node.setData(next.getData());
                    next.setData(temp);
                }
            }
        }
    }

    public String arrayToString() {
        String result = "[";
        for (int i = 0; i < currentIndex + 1; i++) {
            result += "" + select(i) + ",";
        }
        return result.substring(0, result.length() - 1) + "]";
    }
}
